/*
 * Proja - project management software
 * Copyright (C) 2012  Oleg Kasian
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package org.proja.core.entity;

import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.proja.core.entity.customized.ArticleCV;

/**
 *
 * @author Oleg Kasian
 * @version 1.0.0
 * @since 1.0.0
 */
@Entity
@Table(name = "ARTICLES")
public class Article extends AbstractEntity {

    @Id
    @SequenceGenerator(name = "ArticleSeq", sequenceName = "ARTICLES_SEQ")
    @GeneratedValue(generator = "ArticleSeq")
    @Column(name = "ID", updatable = false)
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "article")
    private Set<ArticleCV> customValues;

    @ManyToOne(cascade = CascadeType.DETACH)
    @JoinColumn(name = "PROJECT_ID")
    private Project project;

    @NotNull
    @Column(name = "SUBJECT", nullable = false, length = 255)
    private String subject;

    @Column(name = "SHORTCUT", length = 2000)
    private String shortcut;

    @Lob
    @NotNull
    @Column(name = "BODY", nullable = false)
    private String body;

    @NotNull
    @ManyToOne(cascade = CascadeType.DETACH)
    @JoinColumn(name = "AUTHOR_ID", nullable = false)
    private User author;

    @Column(name = "CREATED_ON", nullable = false, updatable = false)
    @Temporal(TemporalType.DATE)
    private Date createdOn = new Date();

    @Column(name = "UPDATED_ON")
    @Temporal(TemporalType.DATE)
    private Date updatedOn = new Date();

    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }

    public Set<ArticleCV> getCustomValues() {
        return customValues;
    }

    public void setCustomValues(final Set<ArticleCV> customValues) {
        this.customValues = customValues;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(final Project project) {
        this.project = project;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(final String subject) {
        this.subject = subject;
    }

    public String getShortcut() {
        return shortcut;
    }

    public void setShortcut(final String shortcut) {
        this.shortcut = shortcut;
    }

    public String getBody() {
        return body;
    }

    public void setBody(final String body) {
        this.body = body;
    }

    public User getAuthor() {
        return author;
    }

    public void setAuthor(final User author) {
        this.author = author;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(final Date createdOn) {
        this.createdOn = createdOn;
    }

    public Date getUpdatedOn() {
        return updatedOn;
    }

    public void setUpdatedOn(final Date updatedOn) {
        this.updatedOn = updatedOn;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Article other = (Article) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

}
